package com.neuedu.his.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.his.po.Constantitem;
import com.neuedu.his.po.Constanttype;
import com.neuedu.his.po.User;
import com.neuedu.his.service.ConstantitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/constantitem")
public class ConstantItemController {

    @Autowired
    ConstantitemService service;

    @RequestMapping("/list")
    public List<Constantitem> list(){
        return  service.list();
    }

    @RequestMapping("/getbytypeid")
    public List<Constantitem> getbytypeid(@RequestBody Map<String,String>id){
        LambdaQueryWrapper<Constantitem> queryWrapper = new LambdaQueryWrapper<>();
        System.out.println(id.get("id"));
        queryWrapper.eq(Constantitem::getConstanttypeid,Integer.parseInt(id.get("id")));
        return service.list(queryWrapper);
    }

    @RequestMapping("/getbytypeid2")
    public List<Constantitem> getbytypeid2(@RequestParam Integer id){
        System.out.println("getbytypeid2   id="+id);
        QueryWrapper<Constantitem> qw = new QueryWrapper<>();
        qw.eq("constanttypeid",id);
        return service.list(qw);
    }

    @RequestMapping("/getById/{id}")
    public Constantitem getById(@PathVariable String id){
        Constantitem constantitem  = service.getById(id);
        return constantitem;
    }

    @RequestMapping("/add")
    public boolean add(@RequestBody Constantitem type){
        boolean success= service.saveOrUpdate(type);
        return success;
    }
    @RequestMapping("/deleteById/{id}")
    public boolean deleteById(@PathVariable  String id){
        boolean success = service.removeById(id);
        return success;
    }
    @RequestMapping("/search")
    public List<Constantitem> search(@RequestBody Map<String, String> params) {
        String keyword = params.get("keyword");
        String typeIdStr = params.get("typeId");

        LambdaQueryWrapper<Constantitem> queryWrapper = new LambdaQueryWrapper<>();

        // 如果有关键词，搜索常数项编码或名称
        if (keyword != null && !keyword.isEmpty()) {
            queryWrapper.and(qw ->
                    qw.like(Constantitem::getConstantcode, keyword)
                            .or()
                            .like(Constantitem::getConstantname, keyword)
            );
        }

        // 如果有类别ID，添加类别筛选条件
        if (typeIdStr != null && !typeIdStr.isEmpty()) {
            int typeId = Integer.parseInt(typeIdStr);
            queryWrapper.eq(Constantitem::getConstanttypeid, typeId);
        }

        return service.list(queryWrapper);
    }
}
